/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.cqcis.uip.base.framework.service.StreamNameUtil;




/**
 * @author huangmy
 * @date   2007-11-24
 */
public class SocketStreamNameUtil implements StreamNameUtil {	
	private static String RecvNamePreffix = "Recv";
	private static String SendNamePreffix = "Send";	
    private long streamCount;
    private String streamPath;       /*字节流记录路径 /stream*/
    private String intfLogName;         /*接口名*/
    private String preNameSuffix;
    
    public SocketStreamNameUtil() {
    	
    	streamPath = System.getProperty(STREAM_PATH);
    	if(streamPath == null) {
    		streamPath="../logs/stream/";
    	}
    	
    	streamPath = streamPath.trim();
    	
    	if(!(streamPath.endsWith("/"))) {
    		streamPath += "/";
    	}
    }
    
	public String getRecvName() {
    	StringBuffer recvName = new StringBuffer();
    	recvName.append(RecvNamePreffix)
    	        .append(getFileSuffix());
		
    	return recvName.toString();
    }
    
    public String getSendName(String nameSuffix) {    	
    	StringBuffer sendName = new StringBuffer();
    	sendName.append(SendNamePreffix)
    	        .append(nameSuffix);
    	
    	return sendName.toString();
    }
    
    public String getFilePath() {		
    	StringBuffer filePah = new StringBuffer();
    	filePah.append(streamPath).append(intfLogName).append("/")
    	        .append(new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
    	        .append("/");
    	
    	return filePah.toString();
	}

	public String getFileSuffix() {		
		
		StringBuffer fileSuffix = new StringBuffer();
		fileSuffix.append("_")
			      .append(new SimpleDateFormat("HHmmss").format(new Date()))
			      .append("_")
			      .append(String.format("%06d", ++ streamCount))
			      .append(".bak");
		
		preNameSuffix = fileSuffix.toString();
			
		return preNameSuffix;
	}
	
	public String getPreFileSuffix() {	
		return preNameSuffix;
	}

	/**
	 * @param intfLogName
	 */
	public void setIntfLogName(String intfLogName) {
		this.intfLogName = intfLogName;
	}

	public String getRecvName(String nameSuffix) {
		StringBuffer recvName = new StringBuffer();
		recvName.append(RecvNamePreffix)
    	        .append(nameSuffix);
    	
    	return recvName.toString();
	}

	public String getSendName() {
		StringBuffer sendName = new StringBuffer();
		sendName.append(SendNamePreffix)
    	        .append(getFileSuffix());
		
    	return sendName.toString();
	}
}